#!/usr/bin/perl
#
# Hey now..  VPN checker.... Only for use in Gentoo! probably won't work anywhere else.
# But it's bad ass, need to keep that openvpn running.
#
#perl -MCPAN -e shell
#cpan[1]> install File::Touch
#
use Net::Ping;
use File::Touch;

$openvpn_script="openvpn.netherlands";
$host="code.google.com";
$pid_file="/var/run/".$openvpn_script.".pid";

$count=0;

$p = Net::Ping->new("tcp", 2);
if ($p ne "") {
	print "Internet Up!\n";
} else {
	#lets see how many times we were down (stupidly) 4 Minutes
	if (-e "/tmp/down1") {
		$count++;
	} else {
		touch("/tmp/down1");
		exit;
	}
	if (-e "/tmp/down2") {
		$count++;
	} else {
                touch("/tmp/down2");
                exit;
        }
	if (-e "/tmp/down3") {
		$count++;
	} else {
                touch("/tmp/down3");
                exit;
        }
	if ($count == 3) {
		print "Shit we're down....\n";
		$PID=`cat $pid_file`;
		#print $PID;
		`kill -9 $PID`;
		`/etc/init.d/$openvpn_script zap`;
		`/etc/init.d/$openvpn_script start`;
		unlink("/tmp/down1");
		unlink("/tmp/down2");
		unlink("/tmp/down3");

	}
	
}
$p->close();
